package com.alibaba.citrus.cr.b2b.trade.order.repository.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.crb2btradecenter.sdo.OrderLineSDO;
import com.epoch.app.crb2btradecenter.sdo.OrderSDO;
import com.epoch.app.crb2btradecenter.sdo.ScItemSDO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 参数转换
 * @author: qianlei
 * @create: 2021/11/08 22:28
 */
public class OrderConvert {

    public static OrderSDO convertToOrderSDO(Order order, List<OrderLine> orderLines) {
        OrderSDO orderSDO = new OrderSDO();
        orderSDO.setBusinessType(order.getBusinessType());
        orderSDO.setOrderLineSDOS(convertToOrderLineSDOList(orderLines));
        orderSDO.setCustomerCode(order.getCustomerCode());
        orderSDO.setCustomerName(order.getCustomerName());
        orderSDO.setOrganizationCode(order.getOrganizationCode());
        orderSDO.setOrganizationName(order.getOrganizationName());
        orderSDO.setDepartmentCode(order.getDepartmentCode());
        orderSDO.setDepartmentName(order.getDepartmentName());
        orderSDO.setSaleChannelCode(order.getSaleChannelCode());
        orderSDO.setSaleChannelName(order.getSaleChannelName());
        orderSDO.setChannelCode(order.getChannelCode());
        orderSDO.setChannelName(order.getChannelName());
        orderSDO.setReceiveAddressCode(order.getReceiveAddressCode());
        orderSDO.setReceiveDetailAddress(order.getReceiveDetailAddress());
        orderSDO.setTransportModeCode(order.getTransportModeCode());
        orderSDO.setTransportModeName(order.getTransportModeName());
        orderSDO.setId(String.valueOf(order.getId()));
        orderSDO.setFrontOrderId(order.getFrontOrderId());
        orderSDO.setBizCode(order.getBizCode());
        orderSDO.setBusinessCode(order.getBusinessCode());
        orderSDO.setProductGroupCode(order.getProductGroupCode());
        orderSDO.setProductGroupName(order.getProductGroupName());
        orderSDO.setStatus(order.getStatus());
        orderSDO.setCreateReason(order.getCreateReason());
        orderSDO.setDeliveryStatus(order.getDeliveryStatus());
        orderSDO.setEndTime(order.getEndTime());
        Map<String, String> mainFeatures = new HashMap<>();
        if (StringUtils.isNotBlank(order.getFeatures())) {
            mainFeatures = JSON.parseObject(order.getFeatures(), Map.class);
        }
        orderSDO.setFeatures(mainFeatures);
        orderSDO.setBizFeatures(order.getBizFeatures());
        orderSDO.setRemark(order.getRemark());
        orderSDO.setGmtCreate(order.getGmtCreate());
        orderSDO.setGmtModified(order.getGmtModified());
        String creatorId = "";
        if (StringUtils.isNotBlank(order.getCreator())) {
            JSONObject userInfo = JSONObject.parseObject(order.getCreator());
            String empId = userInfo.getString("empId");
            creatorId = StringUtils.isNotBlank(empId) ? empId.replace("CZ_", "") : "";
        }
        orderSDO.setCreatorId(creatorId);
        orderSDO.setCreatorCode(order.getCreatorCode()); // 创建人工号
        orderSDO.setCreatorName(order.getCreatorName());
        orderSDO.setCreatorName2(order.getCreatorName2()); // 创建人名称，业中专用字段
        orderSDO.setModifierName(order.getModifierName());
        orderSDO.setModifierName2(order.getModifierName2()); // 修改人名称，业中专用字段
        orderSDO.setSyncVersion(order.getSyncVersion());
        orderSDO.setOuterOrderId(order.getOuterOrderId());
        orderSDO.setWareHouseCode(order.getWareHouseCode());
        orderSDO.setWareHouseOutCode(order.getWareHouseOutCode());
        orderSDO.setWareHouseName(order.getWareHouseName());
        //换货订单收货仓
        orderSDO.setReceiveWareHouseCode(order.getReceiveWareHouseCode());
        orderSDO.setReceiveWareHouseOutCode(order.getReceiveWareHouseOutCode());
        orderSDO.setReceiveWareHouseName(order.getReceiveWareHouseName());
        orderSDO.setCustomerReferenceNumber(order.getCustomerReferenceNo());
        orderSDO.setCustomerExpectDate(order.getCustomerExpectDate());
        orderSDO.setCostCenter(order.getCostCenter());
        orderSDO.setCashCustomerName(order.getCashCustomerName());
        orderSDO.setOrderType(order.getOrderType());
        orderSDO.setOrderTag(order.getOrderTag());
        orderSDO.setReverseReason(order.getReverseReason());
        orderSDO.setOrderBizId(order.getOrderBizId());
        orderSDO.setOrganizationOutCode(order.getOrganizationOutCode());
        orderSDO.setDepartmentOutCode(order.getDepartmentOutCode());
        orderSDO.setSaleChannelOutCode(order.getSaleChannelOutCode());
        orderSDO.setCustomerPurchaseDate(order.getCustomerPurchaseDate());
        orderSDO.setCustomerAccounts(order.getCustomerAccounts());
        orderSDO.setFromSys(order.getFromSys());
        orderSDO.setBusinessUnitOutCode(order.getBisUnitOutCode());
        orderSDO.setBusinessTypeName(order.getBusinessTypeName());
        orderSDO.setPreOrderNo(order.getPreOrderNo());
        orderSDO.setBisUnitCode(order.getBisUnitCode());
        orderSDO.setBisUnitName(order.getBisUnitName());
        orderSDO.setOrderCategoryCode(order.getOrderCategoryCode());
        orderSDO.setOrderChannelCode(order.getOrderChannelCode());
        orderSDO.setReceiveFactoryCode(order.getReceiveFactoryCode());
        orderSDO.setReceiveStockCode(order.getReceiveStockCode());
        orderSDO.setFulfillmentTypeCode(order.getFulfillmentTypeCode());
        orderSDO.setFulfillmentPriority(order.getFulfillmentPriority());
        orderSDO.setAutoFulfillment(order.getAutoFulfillment());
        orderSDO.setCustomerGrade(order.getCustomerGrade());
        orderSDO.setCustomerGroup(order.getCustomerGroup());
        orderSDO.setSourceOrderNo(order.getSourceOrderNo());
        orderSDO.setRelationNo(order.getRelationNo());
        orderSDO.setCustomerReferenceNo(order.getCustomerReferenceNo());
        orderSDO.setReqNo(order.getReqNo());
        return orderSDO;
    }

    public static List<OrderLineSDO> convertToOrderLineSDOList(List<OrderLine> orderLines) {
        if (CollectionUtils.isEmpty(orderLines)) {
            return new ArrayList<>();
        }
        return orderLines.stream().map(OrderConvert::convertToOrderLineSDO).collect(Collectors.toList());
    }

    public static OrderLineSDO convertToOrderLineSDO(OrderLine orderLine) {
        OrderLineSDO orderLineSDO = new OrderLineSDO();
        orderLineSDO.setPayStatus(orderLine.getPayStatus());
        orderLineSDO.setPayTime(orderLine.getPayTime());
        orderLineSDO.setQuantity(String.valueOf(orderLine.getQuantity()));
        orderLineSDO.setShouldPayFee(StringUtils.isBlank(orderLine.getShouldPayFee()) ? null : Long.valueOf(orderLine.getShouldPayFee()));
        orderLineSDO.setActualPaidFee(StringUtils.isBlank(orderLine.getActualPaidFee()) ? null : Long.valueOf(orderLine.getActualPaidFee()));
        orderLineSDO.setId(String.valueOf(orderLine.getId()));
        orderLineSDO.setMainOrderId(orderLine.getMainOrderId());
        orderLineSDO.setSerialNumber(orderLine.getSerialNumber());
        orderLineSDO.setFrontLineNum(orderLine.getFrontLineNum());
        orderLineSDO.setMainOrderBizId(orderLine.getMainOrderBizId());
        orderLineSDO.setOrderLineTag(orderLine.getOrderLineTag());
        orderLineSDO.setBizCode(orderLine.getBizCode());
        orderLineSDO.setSourceLineNum(orderLine.getSourceLineNum());
        orderLineSDO.setDeliveryFactoryCode(orderLine.getDeliveryFactoryCode());
        orderLineSDO.setDeliveryFactoryName(orderLine.getDeliveryFactoryName());
        orderLineSDO.setStockName(orderLine.getStockName());
        orderLineSDO.setStockCode(orderLine.getStockCode());
        orderLineSDO.setShippingWarehouseName(orderLine.getShippingWarehouseName());
        orderLineSDO.setShippingWarehouseCode(orderLine.getShippingWarehouseCode());
        orderLineSDO.setShippingWarehouseOutCode(orderLine.getShippingWarehouseOutCode());
        orderLineSDO.setOuterOrderLineId(orderLine.getOuterOrderLineId());
        orderLineSDO.setStockConversionNum(orderLine.getStockConversionNum());
        orderLineSDO.setManualPrice(orderLine.getManualPrice());
        orderLineSDO.setManualTotalAmount(orderLine.getManualTotalAmount());
        orderLineSDO.setCustomerCode(orderLine.getCustomerCode());
        orderLineSDO.setStatus(orderLine.getStatus());
        orderLineSDO.setDeliveryStatus(orderLine.getDeliveryStatus());
        orderLineSDO.setDeliveryTime(orderLine.getDeliveryTime());
        orderLineSDO.setEndTime(orderLine.getEndTime());
        orderLineSDO.setActivityName(orderLine.getActivityName());
        orderLineSDO.setActivityCode(orderLine.getActivityCode());
        orderLineSDO.setFrontOrderLineId(orderLine.getFrontOrderLineId());
        orderLineSDO.setRemark(orderLine.getRemark());
        orderLineSDO.setRefuseReasonName(orderLine.getRefuseReasonName());
        orderLineSDO.setRefuseReasonCode(orderLine.getRefuseReasonCode());
        orderLineSDO.setDeliveryAmount(orderLine.getDeliveryAmount());
        orderLineSDO.setGmtCreate(orderLine.getGmtCreate());
        orderLineSDO.setGmtModified(orderLine.getGmtModified());
        Map<String, String> subFeatures = new HashMap<>();
        if (StringUtils.isNotBlank(orderLine.getFeatures())) {
            subFeatures = JSON.parseObject(orderLine.getFeatures(), Map.class);
        }
        orderLineSDO.setFeatures(subFeatures);
        orderLineSDO.setBizFeatures(orderLine.getBizFeatures());
        orderLineSDO.setSyncVersion(orderLine.getSyncVersion());
        orderLineSDO.setCreatorId(orderLine.getCreatorId());
        orderLineSDO.setCreatorName(orderLine.getCreatorName());
        orderLineSDO.setModifierId(orderLine.getModifierId());
        orderLineSDO.setModifierName(orderLine.getModifierName());
        orderLineSDO.setReturnCharacter(orderLine.getReturnCharacter());
        orderLineSDO.setCumulativeReceiptQuantity(orderLine.getCumulativeReceiptQuantity());
        orderLineSDO.setCumulativeProcessingQuantity(orderLine.getCumulativeProcessingQuantity());
        orderLineSDO.setReceiveFactoryName(orderLine.getReceiveFactoryName());
        orderLineSDO.setReceiveFactoryCode(orderLine.getReceiveFactoryCode());
        orderLineSDO.setReceiveStockCode(orderLine.getReceiveStockCode());
        orderLineSDO.setReceiveStockName(orderLine.getReceiveStockName());
        orderLineSDO.setReceiveWarehouseName(orderLine.getReceiveWarehouseName());
        orderLineSDO.setReceiveWarehouseCode(orderLine.getReceiveWarehouseCode());
        orderLineSDO.setReceiveWarehouseOutCode(orderLine.getReceiveWarehouseOutCode());
        orderLineSDO.setSubStatus(orderLine.getSubStatus()); // 子状态，记录F-B订单接收方的状态
        orderLineSDO.setCumulativeNoticeQuantity(orderLine.getCumulativeNoticeQuantity()); // 累计通知数量
        orderLineSDO.setCumulativeWaitOutboundQuantity(orderLine.getCumulativeWaitOutboundQuantity()); // 累计待出库数量
        orderLineSDO.setCumulativeDeliveryQuantity(orderLine.getCumulativeDeliveryQuantity()); // 累计交货数量
        orderLineSDO.setCumulativeReceivedQuantity(orderLine.getCumulativeReceivedQuantity()); // 累计已收货数量
        orderLineSDO.setConfirmQuantity(Objects.isNull(orderLine.getConfirmQuantity()) ? "" : orderLine.getConfirmQuantity().toString()); // 累计已收货数量

        // 物料信息
        ScItemSDO scItemSDO = new ScItemSDO();
        scItemSDO.setUnitConvert(orderLine.getUnitConvert());
        scItemSDO.setSaleUnit(orderLine.getSaleUnit());
        scItemSDO.setInventoryUnitName(orderLine.getInventoryUnit());
        scItemSDO.setInventoryUnitCode(orderLine.getInventoryUnitCode());
        scItemSDO.setScItemId(orderLine.getScItemId());
        scItemSDO.setScItemTitle(orderLine.getScItemTitle());
        scItemSDO.setScItemOutCode(orderLine.getScItemOutCode());
        scItemSDO.setProductGroupCode(orderLine.getProductGroupCode());
        scItemSDO.setProductGroupName(orderLine.getProductGroupName());
        scItemSDO.setUnitChangeRelation(orderLine.getUnitConvert());
        scItemSDO.setSaleUnitName(orderLine.getSaleUnitName());
        scItemSDO.setUnitPrice(StringUtils.isBlank(orderLine.getUnitPrice()) ? null : Long.valueOf(orderLine.getUnitPrice()));
        scItemSDO.setActivityPrice(StringUtils.isBlank(orderLine.getActivityPrice()) ? null : Long.valueOf(orderLine.getActivityPrice()));
        scItemSDO.setQuantity(orderLine.getQuantity());
        scItemSDO.setProductationTime(orderLine.getProductationTime());
        orderLineSDO.setScItemSDO(scItemSDO);

        return orderLineSDO;
    }
}
